import { MutationTypes } from "./mutation-types";
import { UserState } from "./types";
import storage from "store";
import { MutationTree } from 'vuex';

export type Mutations<S = UserState> = {
  [MutationTypes.SET_USER_TOKEN](state: S, payload: UserTokenVO): void,
  [MutationTypes.SET_USERINFORMATION](state: S, payload: UserInformationVO): void
  [MutationTypes.CLEAR_USER_TOKEN](state: S): void
  [MutationTypes.CLEAR_USERINFORMATION](state: S): void
}

export const mutations: MutationTree<UserState> & Mutations = {
  [MutationTypes.SET_USER_TOKEN](state, payload) {
    state.userToken = payload;
    storage.set(MutationTypes.SET_USER_TOKEN, payload);
  },
  [MutationTypes.SET_USERINFORMATION](state, payload) {
    state.userInformation = payload;
    storage.set(MutationTypes.SET_USERINFORMATION, payload);
  },
  [MutationTypes.CLEAR_USER_TOKEN](state) {
    state.userToken = undefined;
    storage.remove(MutationTypes.SET_USER_TOKEN);
  },
  [MutationTypes.CLEAR_USERINFORMATION](state) {
    state.userInformation = undefined;
    storage.remove(MutationTypes.SET_USERINFORMATION);
  },
}
